enable sharing the binary file and use a larger buffer#2786
enable sharing the binary file and use a larger buffer#2786mxinden merged 14 commits intolibp2p:masterfrom
Conversation
mxinden
left a comment
There was a problem hiding this comment.
Thanks for the pull request.
I am in favor of the move to Vec<u8>. I suggest we don't write to a file but continue to write to stdout.
examples/file-sharing.rs
Outdated
| }, | ||
| Get { | ||
| #[clap(long)] | ||
| path: PathBuf, |
There was a problem hiding this comment.
Instead of providing a file path for the retrieved file to be written to, how about leveraging unix pipelines?
E.g. one would do:
cargo run -- get [...] > my_fileWhat do you think?
There was a problem hiding this comment.
Good idea! Thanks for your advices. I've changed the output to stdout().
examples/file-sharing.rs
Outdated
| let mut reader = BufReader::new(File::open(&path)?); | ||
| let mut vec = Vec::<u8>::new(); | ||
| let mut buffer = reader.fill_buf()?; | ||
| let mut len = buffer.len(); | ||
| while len > 0 { | ||
| vec.extend_from_slice(buffer); | ||
| reader.consume(len); | ||
| buffer = reader.fill_buf()?; | ||
| len = buffer.len(); | ||
| } |
There was a problem hiding this comment.
Given that this is reading the entire file into vec, what is the difference to the previous version, i.e. std::fs::read_to_string? If you want to read an entire file as u8, why not use the below?
examples/file-sharing.rs
Outdated
| if request == name { | ||
| let file_content = std::fs::read_to_string(&path)?; | ||
| network_client.respond_file(file_content, channel).await; | ||
| network_client.respond_file(&std::fs::read(&path)?, channel).await; |
There was a problem hiding this comment.
| network_client.respond_file(&std::fs::read(&path)?, channel).await; | |
| network_client.respond_file(std::fs::read(&path)?, channel).await; |
Why borrow here when we clone again later on?
There was a problem hiding this comment.
but expected &Vec<u8> here. any better choice?
Co-authored-by: Max Inden <mail@max-inden.de>
thomaseizinger
left a comment
There was a problem hiding this comment.
LGTM
One small suggestion.
examples/file-sharing.rs
Outdated
| pub async fn respond_file(&mut self, file: &Vec<u8>, channel: ResponseChannel<FileResponse>) { | ||
| self.sender | ||
| .send(Command::RespondFile { file, channel }) | ||
| .send(Command::RespondFile { file: file.to_vec(), channel }) |
There was a problem hiding this comment.
Nit: I think the reference on line 354 is unnecessary if we are going to create a copy anyway and it will remove the diff on line 356.
mxinden
left a comment
There was a problem hiding this comment.
Looks good to me. Thanks for the follow ups.
|
Thanks~ |
Description
Update an examle for trasnfering a binary file
Links to any relevant issues
Open Questions
Change checklist
I have added tests that prove my fix is effective or that my feature worksA changelog entry has been made in the appropriate crates